package io.sodabox.web.utils;

import io.sodabox.web.oauth.utils.AccessGrant;

import java.util.Map;
import java.util.Set;

import org.jboss.netty.handler.codec.http.Cookie;
import org.vertx.java.core.json.JsonObject;


public class CookieUtil {
	
	public static AccessGrant getAccessToken(Set<Cookie> cookies) {

		AccessGrant accessToken = new AccessGrant();
		for (Cookie cookie : cookies) {
			
	        if (cookie.getName().equals("requestToken")) {
	            JsonObject json = new JsonObject(cookie.getValue());
	            accessToken.setKey(json.getString("key"));
	            accessToken.setSecret(json.getString("secret"));
	            accessToken.setProviderId(json.getString("providerId"));
	            JsonObject attrJson = json.getObject("attributes");
	            if(attrJson != null){
	            	Map<String, Object> attributes = attrJson.toMap();
	            	accessToken.setAttributes(attributes);
	            }
	        }
	    }
		
		System.out.println(" >>> 13 - "+accessToken);
		
		return accessToken;

	}
}
